package com.lu.statistics.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lu.statistics.entity.MtDishCategories;
import com.lu.statistics.entity.MtDishDescription;
import com.lu.statistics.entity.MtDishInfo;
import com.lu.statistics.entity.enums.ChoiceTypeEnum;
import com.lu.statistics.entity.enums.ProductTypeEnum;
import com.lu.statistics.entity.front.response.*;
import com.lu.statistics.mapper.MtDishCategoriesMapper;
import com.lu.statistics.mapper.MtDishDescriptionMapper;
import com.lu.statistics.service.DishCategoriesService;
import com.lu.statistics.service.DishInfoService;
import com.lu.statistics.service.GroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishCategoriesServiceImpl extends ServiceImpl<MtDishCategoriesMapper, MtDishCategories> implements DishCategoriesService, GroupService {
    @Autowired
    private MtDishCategoriesMapper dishCategoriesMapper;
    @Autowired
    private DishInfoService dishInfoService;
    @Autowired
    private MtDishDescriptionMapper descriptionMapper;

    @Override
    public IPage<MtDishCategories> queryDishDishCategories(String isAvailable, int pageNo, int pageSize) {
        Page<MtDishCategories> page = new Page(pageNo, pageSize);
        return dishCategoriesMapper.queryDishCategoriesByIsAvailable(page, isAvailable);
    }

    @Override
    public MtDishCategories queryDishCategoriesById(int id) {
        return dishCategoriesMapper.selectById(id);
    }

    @Override
    public DishCategoriesResponse queryDishCategoriesById(int id, int pageNo, int pageSize) {
        MtDishCategories mtDishCategories = dishCategoriesMapper.selectById(id);
        DishCategoriesResponse categoriesResponse = new DishCategoriesResponse();
        BeanUtils.copyProperties(mtDishCategories, categoriesResponse);
        String url = "/api/file/previewPhoto?fileFullPath=" + categoriesResponse.getImgUrl();
        categoriesResponse.setImgUrl(url);
        MtDishInfo dishInfo = new MtDishInfo();
        dishInfo.setCategoriesId(id);
        dishInfo.setChoiceType(ChoiceTypeEnum.SET_MENU.name());
        IPage page = dishInfoService.queryDishInfoList(dishInfo, pageNo, pageSize);
        List<DishInfoResponse> newList = new ArrayList<>();
        if (page.getRecords() != null && page.getRecords().size() > 0) {
            for (Object item : page.getRecords()) {
                MtDishInfo dish = (MtDishInfo) item;
                DishInfoResponse dishInfoResponse = new DishInfoResponse();
                String dishUrl = "/api/file/previewPhoto?fileFullPath=" + dish.getImgUrl();
                dish.setImgUrl(dishUrl);
                BeanUtils.copyProperties(dish, dishInfoResponse);
                newList.add(dishInfoResponse);
            }
            page.setRecords(newList);
            categoriesResponse.setDishPage(page);
        }
        List<MtDishDescription> mtDishDescriptions = descriptionMapper.queryDishDescriptions(id, ChoiceTypeEnum.SET_MENU.name());
        if (mtDishDescriptions != null && mtDishDescriptions.size() > 0) {
            List<DishDescriptionResponse> newDescrList = new ArrayList<>();
            for (MtDishDescription description : mtDishDescriptions) {
                DishDescriptionResponse response = new DishDescriptionResponse();
                BeanUtils.copyProperties(description, response);
                newDescrList.add(response);
            }
            categoriesResponse.setDescList(newDescrList);
        }
        return categoriesResponse;
    }

    @Override
    public ProGroupResponse queryGroupList(int pageNo, int pageSize) {
        ProGroupResponse groupResponse = new ProGroupResponse();
        String isAvailable = "LISTING";
        IPage page = queryDishDishCategories(isAvailable, pageNo, pageSize);
        List<DishCategoriesResponse> newList = new ArrayList<>();
        for (Object item : page.getRecords()) {
            MtDishCategories dishCategories = (MtDishCategories) item;
            String url = "/api/file/previewPhoto?fileFullPath=" + dishCategories.getImgUrl();
            DishCategoriesResponse dishCategoriesResponse = new DishCategoriesResponse();
            BeanUtils.copyProperties(dishCategories, dishCategoriesResponse);
            dishCategoriesResponse.setImgUrl(url);
            newList.add(dishCategoriesResponse);
        }
        groupResponse.setCategorieList(newList);
        groupResponse.setCgTotal(page.getTotal());
        return groupResponse;
    }

    @Override
    public ProductTypeEnum getType() {
        return ProductTypeEnum.MEALSET;
    }
}
